package com.china08.yxyapi.api.home.kecheng.statistical;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;

@CrossOrigin
@RestController
public class CloudAreaLessonListController extends YxyApiBaseController {

    @Autowired
    JdbcTemplate jdbcTemplate;

    @RequestMapping(value = "/api_yw/home/kecheng/cloud/lesson/area/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<RespModel> precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {

        String OrderBy = reqModel.OrderBy;
        String AscOrDesc = reqModel.AscOrDesc;
        if ("ascending".equals(AscOrDesc)) {
            AscOrDesc = "asc";
        } else {
            AscOrDesc = "desc";
        }

        StringBuffer sb = new StringBuffer();
        sb.append(" select                                                      ");
        sb.append("   area_name_district,                                       ");
        sb.append("   lessons_count,                                            ");
        sb.append("   pass_count,                                               ");
        sb.append("   high_quality_count                                        ");
        sb.append(" from yxy_lessons.yxy_lessons_cloud_report_district          ");
        sb.append("  order by " + OrderBy + " " + AscOrDesc + "                 ");
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sb.toString());
        List<RespModel> returnList = new ArrayList<RespModel>();
        for (Map<String, Object> map : list) {
            RespModel respModel = new RespModel();
            respModel.areaNameDistrict = map.get("area_name_district") + "";
            try {
                respModel.lessons_count = Integer.parseInt(map.get("lessons_count") + "");
            } catch (Exception e) {
            }
            try {
                respModel.pass_count = Integer.parseInt(map.get("pass_count") + "");
            } catch (Exception e) {
            }
            try {
                respModel.high_quality_count = Integer.parseInt(map.get("high_quality_count") + "");
            } catch (Exception e) {
            }

            returnList.add(respModel);
        }
        return returnList;
    }

    public static class ReqModel extends YxyApiReqModel {
        public String OrderBy;
        public String AscOrDesc;

        @Override
        public void autoCheck() {
        }
    }

    public static class RespModel extends YxyApiRespModel {
        public String areaNameDistrict;
        public Integer lessons_count = 0;
        public Integer pass_count = 0;
        public Integer high_quality_count = 0;
    }
}
